package com.leruan.aasimp.action.maintenance;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.AprioriEntity;
import com.leruan.aasimp.entity.CarInfo;
import com.leruan.aasimp.entity.CarInsuranceCom;
import com.leruan.aasimp.entity.CarMainInfo;
import com.leruan.aasimp.entity.CarMaintenanceItem;
import com.leruan.aasimp.entity.CarMaintenanceRecord;
import com.leruan.aasimp.entity.CarOdd;
import com.leruan.aasimp.entity.CarParts;
import com.leruan.aasimp.entity.CarPartsImage;
import com.leruan.aasimp.entity.CarPartsSend;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.entity.MtInfoEntity;
import com.leruan.aasimp.entity.PtInfoEntity;
import com.leruan.aasimp.entity.UserTypeEnumEntity;
import com.leruan.aasimp.service.EauserService;
import com.leruan.aasimp.service.MaintenanceService;
import com.leruan.aasimp.util.Apriori;
import com.leruan.aasimp.util.DateUtils;
import com.leruan.aasimp.util.SecurityUtils;
import com.leruan.aasimp.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 维修
 * @author snow
 *
 */
public class MaintenanceAction extends ActionSupport {

	private static final long serialVersionUID = -1906715812583393960L;

	private MaintenanceService maintenanceService;
	private EauserService eauserService;
	
	public String addOdd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		Long id = Long.parseLong(request.getParameter("oddId"));
		String cd = request.getParameter("createDate");
		Date createDate = DateUtils.string2Date(cd);
		String bt = request.getParameter("oddDate");
		Date oddDate = DateUtils.string2Date(bt);
		String cn = request.getParameter("oddCompany");
		String tel = request.getParameter("persontel");
		String email = request.getParameter("personemail");
		String vin = request.getParameter("vin");
		String message = request.getParameter("remark");
		
		CarOdd odd = new CarOdd(id, eauser.getUsername(), tel,
				email, oddDate, createDate,
				cn, vin, "1", message);

		String upid = request.getParameter("id");
		if(upid!=null){
			CarOdd carOdd = maintenanceService.getOddByOid(id);
			odd.setId(carOdd.getId());
			maintenanceService.updateOdd(odd);
			request.setAttribute("message", "修改成功！");
		}else{
			maintenanceService.addOdd(odd);
			request.setAttribute("message", "预约成功！");
		}
		return getOdd();
	}
	public String getOdd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");

		List<CarMainInfo> carMain = eauserService.getCarManagesByUc(eauser.getUsercard());
		if(carMain==null||carMain.size()==0){
			request.setAttribute("info", "对不起，您没有任何车辆！");
			return "main";
		}
		
		JSONArray car = JSONArray.fromObject(carMain);
		request.setAttribute("carMain", car.toString().replace("\"", "'"));
		
		List<CarOdd> odds = maintenanceService.getOdd(eauser.getUsername());

		Collections.sort(odds, new Comparator<CarOdd>() {
			public int compare(CarOdd arg0,
					CarOdd arg1) {
				return ((Timestamp) arg1.getOddDate())
						.compareTo((Timestamp) arg0.getOddDate());
			}
		});
		
		JSONArray array = JSONArray.fromObject(odds);
		System.out.println(array.toString()); 
		request.setAttribute("odds", array.toString().replace("\"", "'"));
		getCompanys();
		return "odd";
	}
	public String deleteodd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		odd.setStatus("0");
		maintenanceService.updateOdd(odd);
		request.setAttribute("message", "删除成功！");
		return getOdd();
	}
	public String showodd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		request.setAttribute("oneOdd", odd);
		return "odd";
	}
	
	public String choiceodd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		CarInsuranceCom companyInfo = (CarInsuranceCom) request.getSession().getAttribute("companyInfo");
		List<CarOdd> odds = maintenanceService.getPassOddByCompanyNm(companyInfo.getIdCompany()+"");
		request.setAttribute("odds", odds);
		return "choiceodd";
	}
	
	public String getMtInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();

		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser!=null&&eauser.getIdCompany()!=null){
			CarInsuranceCom company = eauserService.getComById(eauser.getIdCompany());
			request.getSession().setAttribute("companyInfo", company);
		}
		
		HttpSession session = request.getSession();
		int pageNo = 1;
		if(request.getParameter("no")!=null){
			pageNo = Integer.parseInt(request.getParameter("no"));
		}
		List<CarInfo> infos = maintenanceService.getAllMtInfo(pageNo);
		List<MtInfoEntity> mtinfo = new ArrayList<MtInfoEntity>();
		for(CarInfo o : infos){
			Eauser eauser2 = eauserService.getEauserByUid(o.getUserid());
			System.out.println(eauser2.getUserId());
			CarInsuranceCom company = eauserService.getComById(eauser2.getIdCompany());
			MtInfoEntity mt = new MtInfoEntity();
			mt.setId(o.getId()+"");
			mt.setTitle(o.getTitle());
			mt.setName(o.getName());
			mt.setMobile(o.getMobile()+"");
			if(company!=null){
				mt.setCompanyName(company.getCompanyname());
			}
			mt.setReleaseTime(o.getReleaseTime());
			mt.setPrice(o.getPrice());
			mtinfo.add(mt);
		}
		Long total = maintenanceService.getMtCount();
		session.setAttribute("mtInfos", mtinfo);
		session.setAttribute("pageNoSale", pageNo);
		session.setAttribute("pageNoCenterSale", 3);
		session.setAttribute("totalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
		session.setAttribute("totalSale", total);
		
		getCarsInfo();
		getPtInfo();
		return "index";
	}

	public String getCarsInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser==null){
			return null;
		}
		List<CarMainInfo> carsInfo = maintenanceService.getCarsInfoByIdd(eauser.getUsercard());
		if(carsInfo==null||carsInfo.size()==0){
			return null;
		}
		request.getSession().setAttribute("carMainInfo", carsInfo);
		return "index";
	}

	public String showOneMtInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		CarInfo carInfo = maintenanceService.getMtById(id);

		Eauser eauser = eauserService.getEauserByUid(carInfo.getUserid());
		CarInsuranceCom company = eauserService.getComById(eauser.getIdCompany());
		MtInfoEntity mt = new MtInfoEntity();
		mt.setId(carInfo.getId()+"");
		mt.setTitle(carInfo.getTitle());
		mt.setName(carInfo.getName());
		mt.setMobile(carInfo.getMobile()+"");
		if(company!=null){
			mt.setCompanyName(company.getCompanyname());
		}
		mt.setReleaseTime(carInfo.getReleaseTime());
		mt.setPrice(carInfo.getPrice());
		mt.setInfomain(carInfo.getInfomain());
		request.setAttribute("carInfo", mt);
		return "mtInfo";
	}

	public String getPtInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		int pageNo = 1;
		if(request.getParameter("num")!=null){
			pageNo = Integer.parseInt(request.getParameter("num"));
		}
		List<CarInfo> infos = maintenanceService.getAllPtInfo(pageNo);
		if(infos==null||infos.size()==0){
			return "index";
		}
		List<PtInfoEntity> ptinfo = new ArrayList<PtInfoEntity>();
		for(CarInfo o : infos){
			CarPartsSend ptSend = maintenanceService.getPtByID(o.getTitle());
			CarPartsImage image = maintenanceService.getImgByID(o.getTitle());
			PtInfoEntity pt = new PtInfoEntity();
			if(ptSend!=null){
				pt.setId(o.getId());
				pt.setName(ptSend.getPartsname());
				pt.setCompanyname(ptSend.getFactoryName());
				pt.setNum(ptSend.getNum()+ptSend.getUtil());
				pt.setType(ptSend.getType());
			}
			if(image!=null){
				pt.setImgUrl(image.getImageAddress());
			}
			pt.setPrice(o.getPrice());
			ptinfo.add(pt);
		}
		Long total = maintenanceService.getPtCount();
		session.setAttribute("ptInfos", ptinfo);
		session.setAttribute("pageNumSale", pageNo);
		session.setAttribute("pageNumCenterSale", 3);
		session.setAttribute("totalNumSale", total / UsedCarConstant.PAGE_SIZE + 1);
		session.setAttribute("totalPtSale", total);
		return "index";
	}

	public String showOnePtInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		CarInfo o = maintenanceService.getMtById(id);
		
		CarPartsSend ptSend = maintenanceService.getPtByID(o.getTitle());
		CarPartsImage image = maintenanceService.getImgByID(o.getTitle());
			PtInfoEntity pt = new PtInfoEntity();
			if(ptSend!=null){
				pt.setId(ptSend.getId());
				pt.setName(ptSend.getPartsname());
				pt.setCompanyname(ptSend.getFactoryName());
				pt.setNum(ptSend.getNum()+ptSend.getUtil());
				pt.setType(ptSend.getType());
				
				pt.setPartsId(ptSend.getPartsId());
				pt.setBrand(ptSend.getBrand());
				pt.setUtil(ptSend.getUtil());
			}
			if(image!=null){
				pt.setImgUrl(image.getImageAddress());
			}
			pt.setPrice(o.getPrice());
			pt.setInfoMain(o.getInfomain());
			pt.setMobile(o.getMobile());

			request.setAttribute("carInfo", pt);
		return "ptInfo";
	}
	

	public String getcompany(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser==null){
			request.setAttribute("info", "您还没有登录，请先登录！");
			return "index";
		}
		CarInsuranceCom company = eauserService.getComById(eauser.getIdCompany());
		request.getSession().setAttribute("companyInfo", company);
		return "personInfo";
	}

	public String getCompanys(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Map<String,Object>> companys = new ArrayList<Map<String,Object>>();
		List<Eauser> eausers = eauserService.getEausersByType(UserTypeEnumEntity.MAINTAIN.getValue());
		for(Eauser o : eausers){
			Map<String,Object> map = new HashMap<String, Object>();
			if(o.getIdCompany()==null){
				continue;
			}
			CarInsuranceCom company = eauserService.getComById(o.getIdCompany());
			if(company==null){
				continue;
			}
			map.put("id", company.getIdCompany());
			map.put("name", company.getCompanyname());
			map.put("address", company.getCompanyAdd());
			map.put("username", o.getName());
			map.put("tel", o.getTel());
			companys.add(map);
		}
		
		JSONArray array = JSONArray.fromObject(companys);
		System.out.println(array.toString()); 
		request.setAttribute("companys", array.toString().replace("\"", "'"));
		return "company";
	}
	
	public String getPersons(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		List<Eauser> list = eauserService.getEausersByTypeAndId(10,eauser.getIdCompany());
		JSONArray array = JSONArray.fromObject(list);
		request.setAttribute("WXpersons", array.toString().replace("\"", "'"));
		return "WXpersons";
	}
	
	public String addPersons(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String username = request.getParameter("username");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String usercard = request.getParameter("usercard");
		Long tel = Long.parseLong(request.getParameter("persontel"));
		String userId = StringUtil.getUUID();
		Date registerDate = new Date();
		Eauser person = new Eauser(userId, username, SecurityUtils.encodeByMD5(username), 3l, 0l, 0l, sex,registerDate, 10l, name,usercard, tel,eauser.getIdCompany());
		eauserService.addEauser(person);
		return getPersons();
	}
	
	public String updatePersons(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String usercard = request.getParameter("usercard");
		Long tel = Long.parseLong(request.getParameter("persontel"));
		Eauser eauser = eauserService.getEauserByUid(id);
		eauser.setName(name);
		eauser.setSex(sex);
		eauser.setUsercard(usercard);
		eauser.setTel(tel);
		eauserService.updateEauser(eauser);
		return getPersons();
	}
	
	public String destroyPersons(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		Eauser eauser = eauserService.getEauserByUid(id);
		eauser.setUserType(0l);
		eauserService.updateEauser(eauser);
		return getPersons();
	}
	
	public String updatePwd(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String pwd0 = request.getParameter("pwd0");
		String pwd1 = request.getParameter("pwd1");

		pwd0 = SecurityUtils.encodeByMD5(pwd0);
		pwd1 = SecurityUtils.encodeByMD5(pwd1);
		if (pwd0.equals(eauser.getPassword())) {
			eauser.setPassword(pwd1);
			eauserService.updateEauser(eauser);
			request.setAttribute("pwdmessage", "密码修改成功！");
		} else {
			request.setAttribute("pwdmessage", "旧密码输入有误！");
		}
		return "personInfo";
	}

	public String saveMt() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Long maintainId = Long.parseLong(request.getParameter("mtid"));
		Long oddId = null;
		if(!"".equals(request.getParameter("oddId").trim())){
			oddId = Long.parseLong(request.getParameter("oddId").trim());
		}
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		Date maintainDate = null;
		try {
			maintainDate = fmt.parse(request.getParameter("mtDate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String company = request.getParameter("company");
		String vin = request.getParameter("vin").trim();
		String handperson = ((Eauser) request.getSession().getAttribute(
				"eauser")).getUsername();
		String maintainPrice = request.getParameter("mtprice");
		String partsPrice = request.getParameter("ptprice");
		String toprice = request.getParameter("mttotal");
		String result = request.getParameter("result");
		String remark = request.getParameter("remark");
		CarMaintenanceRecord maintenanceRecord = new CarMaintenanceRecord(
				maintainId, oddId, maintainDate, company, vin, handperson,
				maintainPrice, partsPrice, toprice, remark, result, "1");

		String[] wxbws = request.getParameterValues("wxbw");
		String[] pjbhs = request.getParameterValues("pjbh");
		String[] pjmcs = request.getParameterValues("pjmc");
		String[] sldws = request.getParameterValues("sldw");
		String[] pjsls = request.getParameterValues("pjsl");
		String[] xsdjs = request.getParameterValues("xsdj");
		String[] zjs = request.getParameterValues("zj");
		List<CarMaintenanceItem> items = new ArrayList<CarMaintenanceItem>();
		if(wxbws!=null){
			for (int i = 0; i < wxbws.length; i++) {
				Long itemsId = Long.parseLong(wxbws[i]);
				Long num = null;
				if(!"".equals(pjsls[i])){
					num = Long.parseLong(pjsls[i]);
				}
				CarMaintenanceItem item = new CarMaintenanceItem(maintainId,
						itemsId, pjbhs[i], pjmcs[i], sldws[i], num, xsdjs[i],
						zjs[i]);
				items.add(item);
			}
		}
		CarMaintenanceRecord maintenanceRecord2 = maintenanceService
				.getMaintainByMID(maintainId);
		if (maintenanceRecord2 != null) {
			maintenanceService.deleteItems(maintainId);
			maintenanceRecord.setId(maintenanceRecord2.getId());
			maintenanceService.updateMaintain(maintenanceRecord);
			request.setAttribute("message", "修改成功！");
		} else {
			maintenanceService.addMaintain(maintenanceRecord);
			request.setAttribute("message", "保存成功！");
		}
		if(items!=null&&items.size()!=0){
			maintenanceService.addMtItems(items);
		}
		return showmts();
	}

	public String showmts() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		List<CarMainInfo> carMainInfos = (List<CarMainInfo>) request.getSession().getAttribute("carMainInfo");
		if(eauser==null){
			request.setAttribute("info", "您还没有登录，请先登录！");
			return "index";
		}
		CarInsuranceCom companyInfo = (CarInsuranceCom) request.getSession().getAttribute("companyInfo");
		if(companyInfo!=null){
			String handperson = eauser.getUsername();
			List<CarMaintenanceRecord> maintenanceRecords = maintenanceService
			.getMaintainByUm(handperson);
			
			JSONArray array = JSONArray.fromObject(maintenanceRecords);
			System.out.println(array.toString()); 
			request.setAttribute("maintains", array.toString().replace("\"", "'"));
		}else{
			List<CarMaintenanceRecord> list = new ArrayList<CarMaintenanceRecord>();
			for(CarMainInfo o : carMainInfos){
				List<CarMaintenanceRecord> mr = maintenanceService
				.getMaintainByVin(o.getVin());
				list.addAll(mr);
			}
			JSONArray array = JSONArray.fromObject(list);
			System.out.println(array.toString()); 
			request.setAttribute("maintains", array.toString().replace("\"", "'"));
			request.setAttribute("info", 1);
			
			Map<String , Object> map = new HashMap<String, Object>();
			List<String> price = new ArrayList<String>();
			List<String> time = new ArrayList<String>();
			for(CarMaintenanceRecord o: list){
				price.add(o.getToprice());
				time.add(o.getMaintainDate().toString());
			}
			map.put("time", time);
			map.put("price", price);

			JSONArray array1 = JSONArray.fromObject(map);
			System.out.println(array1.toString()); 
			request.setAttribute("mtprices", array1.toString().replace("\"", "'"));
			
			return "maintainPerson";
		}
		return "maintain";
	}

	public String showTo() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Long maintainId = Long.parseLong(request.getParameter("id"));
		CarMaintenanceRecord maintenanceRecord = maintenanceService
				.getMaintainByMID(maintainId);
		List<CarMaintenanceItem> items = maintenanceService.getItemsById(maintainId);
		request.setAttribute("maintain", maintenanceRecord);
		request.setAttribute("items", items);
		return "mat";
	}

	public String changeTo() {
		showTo();
		return "upmat";
	}

	public String deleteMt() {
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Long maintainId = Long.parseLong(request.getParameter("id"));
		CarMaintenanceRecord maintenanceRecord = maintenanceService
				.getMaintainByMID(maintainId);
		maintenanceRecord.setStatus("0");
		maintenanceService.updateMaintain(maintenanceRecord);
		request.setAttribute("message", "删除成功！");
		return showmts();
	}

	public String saveMtInfo() throws UnsupportedEncodingException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String userid = eauser.getUserId();
		String title = request.getParameter("tl");

		String price = request.getParameter("price");
		String infomain = request.getParameter("infoMain");

		Date releaseTime = new Date();
		String name = eauser.getName();
		Long mobile = eauser.getTel();
		CarInfo carInfo = new CarInfo(userid, "1", title, infomain,
				releaseTime, price, name, mobile, "1");
		maintenanceService.saveMtInfo(carInfo);
		request.setAttribute("messageMt", "发布成功！");
		return "success";
	}
	
	public String saveMrInfo() throws UnsupportedEncodingException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String userid = eauser.getUserId();
		String title = request.getParameter("mrtl");
		
		String price = request.getParameter("mrprice");
		String infomain = request.getParameter("mrinfoMain");
		
		Date releaseTime = new Date();
		String name = eauser.getName();
		Long mobile = eauser.getTel();
		CarInfo carInfo = new CarInfo(userid, "3", title, infomain,
				releaseTime, price, name, mobile, "1");
		maintenanceService.saveMtInfo(carInfo);
		request.setAttribute("messageMt", "发布成功！");
		return "success";
	}

	public String savePtInfo() throws UnsupportedEncodingException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		String userid = eauser.getUserId();
		String paid = new String(request.getParameter("paid").getBytes(
				"iso-8859-1"), "utf-8");
		String paname = new String(request.getParameter("paname").getBytes(
				"iso-8859-1"), "utf-8");
		String brand = new String(request.getParameter("brand").getBytes(
				"iso-8859-1"), "utf-8");

		String type = request.getParameter("type");
		String companyName = new String(request.getParameter("companyName")
				.getBytes("iso-8859-1"), "utf-8");
		Long num = Long.parseLong(request.getParameter("num"));
		String util = new String(request.getParameter("util").getBytes(
				"iso-8859-1"), "utf-8");
		String ptprice = request.getParameter("ptprice");
		String infoPart = new String(request.getParameter("infoPart").getBytes(
				"iso-8859-1"), "utf-8");
		String image = request.getParameter("image");

		Date releaseTime = new Date();
		String name = eauser.getName();
		Long mobile = eauser.getTel();
		CarInfo carInfo = new CarInfo(userid, "2", paid, infoPart,
				releaseTime, ptprice, name, mobile, "1");
		maintenanceService.saveMtInfo(carInfo);
		CarPartsSend carParts = new CarPartsSend(paid, brand, type, paname,
				companyName, num, util, ptprice, "1");
		maintenanceService.addPartSend(carParts);
		request.setAttribute("messageMt", "发布成功！");
		return "success";
	}

	public String choicepart() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<CarParts> parts = maintenanceService.getAllParts();
		request.setAttribute("parts", parts);
		return "success";
	}
	
	public String showMtReport() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<CarMaintenanceItem> mtparts = maintenanceService.getAllMtParts();
		List<Long> mtids = maintenanceService.getAllMaintainIds();
		Map<Long, String> mtmap = new HashMap<Long, String>();
		for(CarMaintenanceItem o: mtparts){
			if(mtmap.get(o.getMaintainId())==null){
				mtmap.put(o.getMaintainId(), "");
			}
			String str = "";
			switch(Integer.parseInt(o.getItemsId()+"")){
				case 1: str = "A";
					break;
				case 2: str = "B";
					break;
				case 3: str = "C";
					break;
				case 4: str = "D";
					break;
				case 5: str = "E";
					break;
				default:
					break;
			}
			mtmap.put(o.getMaintainId(), mtmap.get(o.getMaintainId()) + str);
		}
		String[] transSet = new String[mtids.size()];
		for(int i=0;i<mtids.size();i++){
			transSet[i] =mtmap.get(mtids.get(i));
		}
		Map<String, Double> map = new HashMap<String, Double>();
		map.put("A", 0.4);
		map.put("B", 0.7);
		map.put("C", 0.5);
		map.put("D", 0.9);
		map.put("E", 0.8);
		
		Apriori ap = new Apriori(transSet);
		
		List<AprioriEntity> allapriori = ap.run(0.4,0.2);
		for(int k=0;k<allapriori.size();k++){
			String describe = allapriori.get(k).getDescribe();
			String[] split = describe.split("-->");
			List<String> list1 = new ArrayList<String>();
			for(int i=0;i<split[0].length();i++){
				list1.add(split[0].substring(i, i+1));
			}
			for(int i=0;i<split[1].length();i++){
				list1.add(split[1].substring(i, i+1));
			}
			//计算权系数
			double s=0;
			for(int i=0;i<list1.size();i++){
				s+=map.get(list1.get(i));
			}
			double weight = s/list1.size();
			allapriori.get(k).setWeightConfidence(weight*allapriori.get(k).getConfidence());
			Double count=0.0;
			for(int i=0;i<transSet.length;i++){
				String idType = transSet[i];
				boolean flag = true;
				for(int j=0;j<list1.size();j++){
					if(!idType.contains(list1.get(j))){
						flag = false;
					}
				}
				if(flag){
					count++;
				}
			}
			allapriori.get(k).setSupport(count/transSet.length);
			allapriori.get(k).setWeightSupport(weight*allapriori.get(k).getSupport());
		}
		
		Map<String, Object> data = formatData(allapriori);
		
		
		JSONArray array = JSONArray.fromObject(data);
		System.out.println(array.toString()); 
		request.setAttribute("mtparts", array.toString().replace("\"", "'"));
		request.setAttribute("ptparts", "1");
		request.setAttribute("mtpart", allapriori);
		return "success";
	}
	
	private Map<String, Object> formatData(List<AprioriEntity> allapriori) {
		Map<String,Object> data = new HashMap<String, Object>();
		List<String> describe = new ArrayList<String>();
		List<Double> confidence = new ArrayList<Double>();
		List<Double> weightConfidence = new ArrayList<Double>();
		List<Double> support = new ArrayList<Double>();
		List<Double> weightSupport = new ArrayList<Double>();
		for(AprioriEntity o :allapriori){
			describe.add(o.getDescribe());
			double a = Math.round(o.getConfidence()*1000);
			confidence.add(a/1000);
			double b = Math.round(o.getWeightConfidence()*1000);
			weightConfidence.add(b/1000);
			double c = Math.round(o.getSupport()*1000);
			support.add(c/1000);
			double d = Math.round(o.getWeightSupport()*1000);
			weightSupport.add(d/1000);
		}
		data.put("describe", describe);
		data.put("confidence", confidence);
		data.put("weightConfidence", weightConfidence);
		data.put("support", support);
		data.put("weightSupport", weightSupport);
		return data;
	}

	public String showPtReport() {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<CarMaintenanceItem> mtparts = maintenanceService.getAllMtParts();
		List<Long> mtids = maintenanceService.getAllMaintainIds();
		Map<Long, String> mtmap = new HashMap<Long, String>();
		for(CarMaintenanceItem o: mtparts){
			if(o.getPartId()==null||"".equals(o.getPartId())){
				continue;
			}
			if(mtmap.get(o.getMaintainId())==null){
				mtmap.put(o.getMaintainId(), "");
			}
			String str = "";
			String partId = o.getPartId();
			if("ZDLBJ146".equals(partId)){str = "A";}
			else if("ZDLBJ147".equals(partId)){str = "B";}
			else if("ZDLBJ148".equals(partId)){str = "C";}
			else if("ZDLBJ149".equals(partId)){str = "D";}
			else if("ZDLBJ140".equals(partId)){str = "E";}
			else if("ZDLBJ144".equals(partId)){str = "F";}
			else if("ZDLBJ143".equals(partId)){str = "G";}
			else if("ZDLBJ142".equals(partId)){str = "H";}
			else if("ZDLBJ141".equals(partId)){str = "I";}
			else if("ZDLBJ145".equals(partId)){str = "J";}

			else if("QCDQ360".equals(partId)){str = "K";}
			else if("QCDQ304".equals(partId)){str = "L";}
			else if("QCDQ498".equals(partId)){str = "M";}
			else if("QCDQ389".equals(partId)){str = "N";}
			else if("QCDQ470".equals(partId)){str = "O";}
			else if("QCDQ454".equals(partId)){str = "P";}
			else if("QCDQ334".equals(partId)){str = "Q";}
			else if("QCDQ42".equals(partId)){str = "R";}
			else if("QCDQ341".equals(partId)){str = "S";}
			else if("QCDQ35".equals(partId)){str = "T";}
			
			else if("FDJ335".equals(partId)){str = "U";}
			else if("FDJ345".equals(partId)){str = "V";}
			else if("FDJ346".equals(partId)){str = "W";}
			else if("FDJ347".equals(partId)){str = "X";}
			else if("FDJ348".equals(partId)){str = "Y";}
			else if("FDJ349".equals(partId)){str = "Z";}
			else if("FDJ340".equals(partId)){str = "a";}
			else if("FDJ344".equals(partId)){str = "b";}
			else if("FDJ343".equals(partId)){str = "c";}
			else if("FDJ342".equals(partId)){str = "d";}
			else if("FDJ341".equals(partId)){str = "e";}
			
			else if("QCDP446".equals(partId)){str = "f";}
			else if("QCDP747".equals(partId)){str = "g";}
			else if("QCDP358".equals(partId)){str = "h";}
			else if("QCDP329".equals(partId)){str = "i";}
			else if("QCDP310".equals(partId)){str = "j";}
			else if("QCDP384".equals(partId)){str = "k";}
			else if("QCDP43".equals(partId)){str = "l";}
			else if("QCDP345".equals(partId)){str = "m";}
			else if("QCDP33".equals(partId)){str = "n";}
			else if("QCDP330".equals(partId)){str = "o";}
			
			else if("QCCS3445".equals(partId)){str = "p";}
			else if("QCCS3466".equals(partId)){str = "q";}
			else if("QCCS3847".equals(partId)){str = "r";}
			else if("QCCS3428".equals(partId)){str = "s";}
			else if("QCCS4349".equals(partId)){str = "t";}
			else if("QCCS3460".equals(partId)){str = "u";}
			else if("QCCS3844".equals(partId)){str = "v";}
			else if("QCCS0343".equals(partId)){str = "w";}
			else if("QCCS3242".equals(partId)){str = "x";}
			else if("QCCS3141".equals(partId)){str = "y";}
			
//			A:刹车盘,B压缩机,C制动室,D真空助力器,E制动总泵,F电动液压泵,G刹车油管,H高压油管,I刹车线,J刹车片
//			K雨刮电机,L风扇电机,M点火线圈,N电子调节器,O汽车仪表,P电容器分电器,Q应急灯开关,R火花塞,S分电器盖,T发电机
//			U转向盘,V油管,W齿圈,X气缸垫,Y消声器,Z燃油泵,a气门推杆,b传感器,c气门导管,d密封垫,e空气流量计
//			f动力转向活塞,g液压泵,h离合器,i减震器,j直拉杆,k汽车轴承,l左右横拉杆,m万向节,n调整臂,o轮胎
//			p前大灯,q尾灯,r转向灯,s雾灯,t保险杆,u门把手,v中网,w后视镜,x内视镜,y灯泡,
			
			mtmap.put(o.getMaintainId(), mtmap.get(o.getMaintainId()) + str);
		}
		List<String> transSet = new ArrayList<String>();
		for(int i=0;i<mtids.size();i++){
			if(mtmap.get(mtids.get(i))!=null){
				transSet.add(mtmap.get(mtids.get(i)));
			}
		}
		Map<String, Double> map = new HashMap<String, Double>();
		map.put("A", 0.4);
		map.put("B", 0.7);
		map.put("C", 0.5);
		map.put("D", 0.9);
		map.put("E", 0.8);
		map.put("F", 0.6);
		map.put("G", 0.7);
		map.put("H", 0.5);
		map.put("I", 0.9);
		map.put("J", 0.3);
		map.put("K", 0.2);
		map.put("L", 0.7);
		map.put("M", 0.6);
		map.put("N", 0.4);
		map.put("O", 0.9);
		map.put("P", 0.3);
		map.put("Q", 0.7);
		map.put("R", 0.8);
		map.put("S", 0.6);
		map.put("T", 0.7);
		map.put("U", 0.5);
		map.put("V", 0.4);
		map.put("W", 0.6);
		map.put("X", 0.7);
		map.put("Y", 0.5);
		map.put("Z", 0.4);
		map.put("a", 0.4);
		map.put("b", 0.7);
		map.put("c", 0.5);
		map.put("d", 0.9);
		map.put("e", 0.8);
		map.put("f", 0.5);
		map.put("g", 0.4);
		map.put("h", 0.3);
		map.put("i", 0.5);
		map.put("j", 0.6);
		map.put("k", 0.7);
		map.put("l", 0.7);
		map.put("m", 0.4);
		map.put("n", 0.5);
		map.put("o", 0.8);
		map.put("p", 0.6);
		map.put("q", 0.4);
		map.put("r", 0.7);
		map.put("s", 0.7);
		map.put("t", 0.6);
		map.put("u", 0.6);
		map.put("v", 0.5);
		map.put("w", 0.5);
		map.put("x", 0.4);
		map.put("y", 0.8);
		
		String[] transSet1 = new String[transSet.size()];
		for(int i=0;i<transSet.size();i++){
			transSet1[i] = transSet.get(i);
		}
		Apriori ap = new Apriori(transSet1);
		
		List<AprioriEntity> allapriori = ap.run(0.12,0.3);
		for(int k=0;k<allapriori.size();k++){
			String describe = allapriori.get(k).getDescribe();
			String[] split = describe.split("-->");
			List<String> list1 = new ArrayList<String>();
			for(int i=0;i<split[0].length();i++){
				list1.add(split[0].substring(i, i+1));
			}
			for(int i=0;i<split[1].length();i++){
				list1.add(split[1].substring(i, i+1));
			}
			//计算权系数
			double s=0;
			for(int i=0;i<list1.size();i++){
				s+=map.get(list1.get(i));
			}
			double weight = s/list1.size();
			allapriori.get(k).setWeightConfidence(weight*allapriori.get(k).getConfidence());
			Double count=0.0;
			for(int i=0;i<transSet1.length;i++){
				String idType = transSet1[i];
				boolean flag = true;
				for(int j=0;j<list1.size();j++){
					if(!idType.contains(list1.get(j))){
						flag = false;
					}
				}
				if(flag){
					count++;
				}
			}
			allapriori.get(k).setSupport(count/transSet1.length);
			allapriori.get(k).setWeightSupport(weight*allapriori.get(k).getSupport());
		}

		Map<String, Object> data = formatData(allapriori);

		JSONArray array = JSONArray.fromObject(data);
		System.out.println(array.toString()); 
		request.setAttribute("ptparts", array.toString().replace("\"", "'"));
		request.setAttribute("mtparts", "1");
		request.setAttribute("ptpart", allapriori);
		return "success";
	}

	public MaintenanceService getMaintenanceService() {
		return maintenanceService;
	}

	public void setMaintenanceService(MaintenanceService maintenanceService) {
		this.maintenanceService = maintenanceService;
	}
	
	public EauserService getEauserService() {
		return eauserService;
	}

	public void setEauserService(EauserService eauserService) {
		this.eauserService = eauserService;
	}
}
